<?php

declare(strict_types=1);

namespace App\Command;

use Elasticsearch\Client;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Elasticsearch\ClientBuilderFactory;
use Hyperf\Utils\Coroutine;
use Hyperf\Utils\Parallel;
use Psr\Container\ContainerInterface;
use Hyperf\Command\Annotation\Command;

/**
 * @Command
 */
class Zjses extends HyperfCommand
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;

        parent::__construct('zjses');
    }

    public function configure()
    {
        parent::configure();
        $this->setDescription('质监所ES初始化');
    }

    public function handle()
    {
        $max_page = 43;
//        $max_page = 4;

        $client = new \GuzzleHttp\Client();
        $parallel = new Parallel();
//        $url = 'http://dev-api.shlyts.cn/es?page='; //dev
        $url = 'http://api.shlyts.cn/es?page='; //prod
        $output = $this->output;
        //先手动执行第一页
        $output->progressStart($max_page * 5000);
        for ($page = 2; $page <= $max_page; $page ++) {
            $parallel->add(function() use ($page, $client, $url, $output) {
                $data = json_decode($client->get($url.$page)->getBody()->getContents(), true);
                $this->line($data);
                if (!isset($data['count'])) return;
                $output->progressAdvance($data['count']);
            });
        }
        $parallel->wait();
        $this->output->progressFinish();
    }
}
